﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Infrastructure.ObjectModel;
using B10_Ex02_Elad_034695114_Meyron_026607788.ObjectModel;
using B10_Ex02_EladLebovitch_034695114.Interfaces;

namespace B10_Ex02_Elad_034695114_Meyron_026607788
{
    /// <summary>
    /// Enemy sprite Class
    /// </summary>
    public class EnemySprite : EnemySpriteBase
    {
        #region Data Members

        private const int k_FireFrequency = 5000;
        private const int k_MaxBullets = 2;
        private const int k_EnemyTextureSize = 32;
        private const int k_ShrinkSeconds = 1000;
        private List<EnemyBullet> m_Bullets;

        #endregion

        public List<EnemyBullet> Bullets
        {
            get
            {
                return m_Bullets;
            }

            protected set
            {
                m_Bullets = value;
            }
        }

        #region Ctor

        /// <summary>
        /// Constructs an enemy sprite
        /// </summary>
        /// <param name="i_game">The game</param>
        /// <param name="i_assetName">The texture</param>
        /// <param name="i_tint">The color of the enemy</param>
        public EnemySprite(Game i_game, string i_assetName, int i_assetRow, int i_assetCol, Color i_tint, int i_points)
            : base(i_assetName, i_game)
        {
            this.TintColor = i_tint;
            this.Points = i_points;
            this.ShrinkSeconds = k_ShrinkSeconds;
            this.Bullets = new List<EnemyBullet>();
            this.SourceRectangle = new Rectangle(i_assetRow * k_EnemyTextureSize,
                                                 i_assetCol * k_EnemyTextureSize, k_EnemyTextureSize, k_EnemyTextureSize);
        }

        protected override void InitBounds()
        {
            this.m_WidthBeforeScale = this.SourceRectangle.Width;
            this.m_HeightBeforeScale = this.SourceRectangle.Height;
            this.PositionOrigin = new Vector2(this.SourceRectangle.Width / 2, this.SourceRectangle.Height / 2);
            this.RotationOrigin = new Vector2(this.SourceRectangle.Width / 2, this.SourceRectangle.Height / 2);
        }

        protected override void InitSourceRectangle()
        {
        }

        #endregion

        #region Other Methods

        public override void Update(GameTime gameTime)
        {
            // Randomly decide firing
            handleBullets();
            base.Update(gameTime);
        }

        /// <summary>
        /// Fires a bullet to the top
        /// </summary>
        protected void handleBullets()
        {
            // Check for passed Bullets and removes them from the collection
            for (int nBulletIndex = 0; nBulletIndex < this.Bullets.Count; nBulletIndex++)
            {
                if (!this.Bullets[nBulletIndex].Enabled)
                {
                    this.Bullets.RemoveAt(nBulletIndex);
                    nBulletIndex--;
                }
            }

            // Randomly decide firing only if the number of bullets is less than k_maxBullets
            if  ((((UtilityService)(Game.Services.GetService(typeof(UtilityService)))).RandomCondition(k_FireFrequency)) && 
                (this.Bullets.Count < k_MaxBullets))
            {
                EnemyBullet bltNew = new EnemyBullet(
                     this.Game,
                     new Vector2(this.PositionOfOrigin.X + (this.Width / 2), this.PositionOfOrigin.Y + (this.Height / 2)));
                m_Bullets.Add(bltNew);
            }
        }

        public override void Collided(Infrastructure.Managers.ICollidable i_Collidable)
        {
            // Check if it's a user bullet
            if (i_Collidable is IPlayerObjects && i_Collidable is IHarmful)
            {
                // Set visiblity and kill ourselves
                this.m_IsDying = true;
            }
        }

        public void animate()
        {
            this.SourceRectangle = new Rectangle((this.SourceRectangle.X + k_EnemyTextureSize) % (k_EnemyTextureSize * 2),
                                                  this.SourceRectangle.Y, k_EnemyTextureSize, k_EnemyTextureSize);
        }

        #endregion
    }
}